<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends CI_Controller {

    var $usr_admin;
    var $pwd_admin;
    var $lastlog_admin;

    public function getUsr_admin() {
        return $this->usr_admin;
    }

    public function getPwd_admin() {
        return $this->pwd_admin;
    }

    public function getLastlog_admin() {
        return $this->lastlog_admin;
    }

    public function setUsr_admin($usr_admin) {
        $this->usr_admin = $usr_admin;
    }

    public function setPwd_admin($pwd_admin) {
        $this->pwd_admin = $pwd_admin;
    }

    public function setLastlog_admin($lastlog_admin) {
        $this->lastlog_admin = $lastlog_admin;
    }

    public function __construct() {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model(array('m_admin', 'm_category', 'm_location', 'm_post', 'm_user'));
    }

    public function index() {
        $data['title'] = "Dashboard";
        $data['page'] = "home";

        $data['location'] = $this->m_location->get_all_location();
        $data['category'] = $this->m_category->get_all_category();
        $data['post'] = $this->m_post->select_all_post();
        $data['user'] = $this->m_user->get_all_user();

        $this->load->view('admin/homepage', $data);
    }

    public function proses_update_admin($param){
        if($param != ""){
            $this->load->library('form_validation');

            if($this->input->post('') != $_COOKIE['c_usrname_adm']){
                $this->form_validation->set_rules('username_adm', 'Username', 'required|alpha_dash|alpha_numeric|is_unique[admin.username_admin]');
            }
            $this->form_validation->set_rules('username_adm', 'Username', 'required|alpha_dash|alpha_numeric|');
            $this->form_validation->set_rules('password_adm', 'Password');
            $this->form_validation->set_rules('re_password_adm', 'Re-type Password', 'matches[password_adm]');

            if ($this->form_validation->run() != FALSE){
                $data_adm['username'] =  $this->input->post('username_adm');
                if($this->input->post('password_adm') != ""){
                    $data_adm['password'] = md5($this->input->post('password_adm'));
                }else{
                    $data_adm['password'] = $_COOKIE['c_pwd_adm'];
                }
                $data_adm['id'] =  $_COOKIE['c_id_adm'];

                $cek_adm =  $this->m_admin->update_admin($data_adm);

                if($cek_adm){
                    $this->session->set_flashdata('notif_sts', '1');
                    $this->session->set_flashdata('notif', 'Updating Profile Success');
                }else{
                    $this->session->set_flashdata('notif_sts', '0');
                    $this->session->set_flashdata('notif', 'Updating Profile Failed');
                }
            }else{
                $this->session->set_flashdata('notif_sts', '0');
                $this->session->set_flashdata('notif', validation_errors());
            }
        }
        
        redirect("Admin");
    }

    function user() {
        $data['title'] = "Users Config";
        $data['page'] = "user";

        $data['location'] = $this->m_location->get_all_location();
        $data['category'] = $this->m_category->get_all_category();
        $data['post'] = $this->m_post->select_all_post();
        $data['user'] = $this->m_user->get_all_user();

        $this->load->view('admin/homepage', $data);
    }

    /*function edit_user($param){
        $data['title'] = "Users Config";
        $data['page'] = "user";

        $data['location'] = $this->m_location->get_all_location();
        $data['category'] = $this->m_category->get_all_category();
        $data['post'] = $this->m_post->select_all_post();
        $data['user'] = $this->m_user->get_all_user();

        $this->load->view('admin/homepage', $data);
    }*/

    function delete_user($param){
        $data['title'] = "Delete User";
        $data['page'] = "user_delete";

        $data['location'] = $this->m_location->get_all_location();
        $data['category'] = $this->m_category->get_all_category();
        $data['post'] = $this->m_post->select_all_post();
        $data['user'] = $this->m_user->get_all_user();

        $data['user_data'] = $this->m_user->select_user_byId($param);
        //print_r($data['user_data']);
        $this->load->view('admin/homepage', $data);
    }

    function delete_user_process($param){
        if($this->input->post('btn_delete_user') != ""){
            $cek = $this->m_user->delete_user($param);
            if($cek){
                $this->session->set_flashdata('message_type', '1');
                $this->session->set_flashdata('message', 'Deleting User Success');
            }else{
                $this->session->set_flashdata('message_type', '0');
                $this->session->set_flashdata('message', 'Deleting User Failed');
            }
            redirect("Admin/user");
        }else{
            redirect('Admin/user');
        }
    }

    function post_page($id=0){
        $jml = $this->db->get('post');
        //pengaturan pagination
        $config['base_url'] = site_url().'/Admin/post_page';
        $config['total_rows'] = $jml->num_rows();
        $config['per_page'] = '5';
        $config['num_links'] = 2;

        $config['next_link'] = '<i class="fa fa-forward"></i>';
        $config['prev_link'] = '<i class="fa fa-backward"></i>';
        $config['first_link'] = '<i class="fa fa-step-backward"></i>';
        $config['last_link'] = '<i class="fa fa-step-forward"></i>';

        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#" style="color:#fff;">';
        $config['cur_tag_close'] = '</li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        //inisialisasi config
        $this->pagination->initialize($config);

        //buat pagination
        $data['halaman'] = $this->pagination->create_links();

        $data['query'] = $this->m_post->get_post_page($config['per_page'], $id);

        $data['location'] = $this->m_location->get_all_location();
        $data['category'] = $this->m_category->get_all_category();
        $data['post'] = $this->m_post->select_all_post();
        $data['user'] = $this->m_user->get_all_user();
        $data['title'] = "Posts Details";
        $data['page'] = "post";

        $this->load->view('admin/homepage', $data);
    }

    function edit_post($param){
        $data['location'] = $this->m_location->get_all_location();
        $data['category'] = $this->m_category->get_all_category();
        $data['post'] = $this->m_post->select_all_post();
        $data['user'] = $this->m_user->get_all_user();
        $data['title'] = "Edit Post";
        $data['page'] = "post_edit";
        $data['result'] = $this->m_post->select_post_byId($param);

        $this->load->view('admin/homepage', $data);
    }

    function delete_post($param){
        $data['location'] = $this->m_location->get_all_location();
        $data['category'] = $this->m_category->get_all_category();
        $data['post'] = $this->m_post->select_all_post();
        $data['user'] = $this->m_user->get_all_user();
        $data['title'] = "Delete Post";
        $data['page'] = "post_delete";
        $data['result'] = $this->m_post->select_post_byId($param);

        $this->load->view('admin/homepage', $data);
    }
    function location($id=0){
        $jml = $this->db->get('location');
        //pengaturan pagination
        $config['base_url'] = site_url().'/Admin/location';
        $config['total_rows'] = $jml->num_rows();
        $config['per_page'] = '8';

        $config['next_link'] = '<i class="fa fa-forward"></i>';
        $config['prev_link'] = '<i class="fa fa-backward"></i>';
        $config['first_link'] = '<i class="fa fa-step-backward"></i>';
        $config['last_link'] = '<i class="fa fa-step-forward"></i>';

        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#" style="color:#fff;">';
        $config['cur_tag_close'] = '</li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        //inisialisasi config
        $this->pagination->initialize($config);

        //buat pagination
        $data['halaman'] = $this->pagination->create_links();

        $data['pager'] = $this->m_location->get_location_page($config['per_page'], $id);
        

        $data['title'] = "Location Details";
        $data['page'] = "loc";

        $data['location'] = $this->m_location->get_all_location();
        $data['category'] = $this->m_category->get_all_category();
        $data['post'] = $this->m_post->select_all_post();
        $data['user'] = $this->m_user->get_all_user();

        $this->load->view('admin/homepage', $data);   
    }

    function add_location() {
        if($this->input->post('btn_add_loc') !=""){
            $loc_data['name'] = $this->input->post('location_name');
            $loc_data['latitude'] = $this->input->post('latitude');
            $loc_data['longitude'] = $this->input->post('longitude');
            $loc_data['admin'] = $_COOKIE['c_id_adm'];
            
            $cek  = $this->m_location->insert_location($loc_data);
            if($cek == TRUE){
                redirect('Admin/location');
            }else{
                echo "<script>alert('Insert Data Failed')</script>";
            }
            
            $data['title'] = "Locations List";
            $data['page'] = "loc";

            $data['location'] = $this->m_location->get_all_location();
            $data['category'] = $this->m_category->get_all_category();
            $data['post'] = $this->m_post->select_all_post();
            $data['user'] = $this->m_user->get_all_user();
        }else{
            redirect('Admin/location');
        }
        
    }

    function edit_location($param) {
        if($param != ""){
            $data['a_loc'] = $this->m_location->get_location_byID($param);
            
            $data['location'] = $this->m_location->get_all_location();
            $data['category'] = $this->m_category->get_all_category();
            $data['post'] = $this->m_post->select_all_post();
            $data['user'] = $this->m_user->get_all_user();


            $data['title'] = "Edit Location";
            $data['page'] = "loc_edit";
            $this->load->view('admin/homepage', $data);  
        }else{
            redirect("Admin/location");
        }
    }

    function delete_location($param){
        if($param != ""){
            $cek_existing = $this->m_location->get_location_byID($param);
            if(count($cek_existing) > 0){
                $data['location'] = $this->m_location->get_all_location();
                $data['category'] = $this->m_category->get_all_category();
                $data['post'] = $this->m_post->select_all_post();
                $data['user'] = $this->m_user->get_all_user();

                $data['a_loc'] = $this->m_location->get_location_byID($param);

                $data['title'] = "Delete Location";
                $data['page'] = "loc_delete";
                $this->load->view('admin/homepage', $data);  
            }else{
                redirect("Admin/location");
            }
            
        }else{
            redirect("Admin/location");
        }
    }

    function category($id=0) {
        $jml = $this->db->get('category');
        //pengaturan pagination
        $config['base_url'] = site_url().'/Admin/category';
        $config['total_rows'] = $jml->num_rows();
        $config['per_page'] = '10';
        $config['num_links'] = 2;
        $config['display_pages'] = FALSE; 

        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['first_link'] = '<i class="fa fa-step-backward"></i>';
        $config['last_link'] = '<i class="fa fa-step-forward"></i>';

        $config['full_tag_open'] = '<ul class="pager">';
        $config['full_tag_close'] = '</ul>';
        
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        

        //inisialisasi config
        $this->pagination->initialize($config);

        //buat pagination
        $data['halaman'] = $this->pagination->create_links();

        $data['pager'] = $this->m_category->get_category_page($config['per_page'], $id);
        $data['title'] = "Category Details";
        $data['page'] = "cat";

        $data['location'] = $this->m_location->get_all_location();
        $data['category'] = $this->m_category->get_all_category();
        $data['post'] = $this->m_post->select_all_post();
        $data['user'] = $this->m_user->get_all_user();

        $this->load->view('admin/homepage', $data);
    }

    function edit_category($param) {
        if($param != "" || !isset($param)){
            $data['result'] = $this->m_category->select_category_byID($param);
            $data['title'] = "Edit Category";
            $data['page'] = "cat_edit";

            $data['location'] = $this->m_location->get_all_location();
            $data['category'] = $this->m_category->get_all_category();
            $data['post'] = $this->m_post->select_all_post();
            $data['user'] = $this->m_user->get_all_user();

            $this->load->view('admin/homepage', $data);
        }else{
            redirect('Admin/category');
        }
    }

    function delete_category($param){
        if($param != "" || !isset($param)){
            $data['result'] = $this->m_category->select_category_byID($param);
            $data['title'] = "Delete Category";
            $data['page'] = "cat_delete";

            $data['location'] = $this->m_location->get_all_location();
            $data['category'] = $this->m_category->get_all_category();
            $data['post'] = $this->m_post->select_all_post();
            $data['user'] = $this->m_user->get_all_user();

            $this->load->view('admin/homepage', $data);
        }else{
            redirect('Admin/category');   
        }
    }

    //Login Controll
    function login() {
        $this->load->view('adm_login');
    }

    function do_login() {
        if($this->input->post('btn_login') != ""){
            $this->load->library('form_validation');

            $this->form_validation->set_rules('frm_username', 'Username', 'required|alpha_numeric|alpha_dash');
            $this->form_validation->set_rules('frm_password', 'Password', 'required');

            if ($this->form_validation->run() != FALSE){
                $this->setUsr_admin($this->input->post('frm_username'));
                $this->setPwd_admin(md5($this->input->post('frm_password')));
                $data['usr'] = $this->getUsr_admin();
                $data['pwd'] = $this->getPwd_admin();

                $data['admin'] = $this->m_admin->login_check($data);
                $this->setUsr_admin($data['admin']['username_admin']);
                $this->setPwd_admin($data['admin']['password_admin']);
                print_r($this->m_admin->login_check($data));
                if ($data['admin'] != false) {
                    $this->setCookies('c_usrname_adm', $this->getUsr_admin());
                    $this->setCookies('c_pwd_adm', $this->getPwd_admin());
                    $this->setCookies('c_id_adm', $data['admin']['id_admin']);
                    $this->m_admin->setlast_log_adm($this->getUsr_admin());
                    redirect('Admin');
                } else {
                    $this->session->set_flashdata('login_fail', 'Username or Password maybe wrong');
                    redirect('Admin/login');
                }
            }else{
                $this->session->set_flashdata('login_fail', validation_errors());
                redirect('Admin/login');
            }
        }else{
            redirect('Admin/login');
        }
        
    }

    //Cookies Controll
    function setCookies($cookie_name, $value) {
        $this->input->set_cookie($cookie_name, $value, time() + 3600);
    }

    public function get_cookie() {
        return $_COOKIE;
    }

    function validate_cookie() {
        if (isset($_COOKIE['c_usrname_adm']) && isset($_COOKIE['c_pwd_adm'])) {
            if ($_COOKIE['c_usrname_adm'] != "" && $_COOKIE['c_pwd_adm'] != "") {
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    function logout() {
        $this->input->set_cookie('c_usrname_adm', '');
        $this->input->set_cookie('c_pwd_adm', '');
        redirect('Admin/login');
    }

}
